package com.kangzhan.handler;

import com.kangzhan.entiy.ReturnEntity;
import com.kangzhan.utils.ReturnUtil;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

// @ControllerAdvice
public class DefaultExceptionHandler {

    // 捕获Controller抛出的空指针异常
    @ResponseBody
    @ExceptionHandler(NullPointerException.class)
    public ReturnEntity handlerNullException() {
        // 封装异常信息为RespinseResult对象
        return ReturnUtil.fail(500,"你遇到了空指针异常");
    }

    // 兜底异常，处理所有代码中未考虑到的异常
    @ResponseBody
    @ExceptionHandler(Exception.class)
    public ReturnEntity handlerException() {
        // 封装异常信息为RespinseResult对象
        return ReturnUtil.fail(500,"这是一个最大的异常");
    }
}